package com.shd.edanpei.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shd.edanpei.R;
import com.shd.edanpei.ui.adapter.ShareOrderAdapter;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.presenter.contract.ShareOrderContract;
import com.shd.edanpei.presenter.ShareOrderPresenter;
import com.shd.edanpei.view.RefreshLayout;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by mc on 2018/10/19.
 * yang-wen-wen
 */

public class MoreOrderActivity extends BaseActivity<ShareOrderPresenter> implements ShareOrderContract.ShareOrderIView {

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.refresh_layout)
    RefreshLayout refreshLayout;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    private ShareOrderAdapter mAdapter;
    private Bundle bundle = new Bundle();

    @Override
    protected int setLayout() {
        return R.layout.activity_more_order;
    }

    @Override
    protected ShareOrderPresenter bindPresenter() {
        return new ShareOrderPresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        headline.setText("更多订单");
        RxBus.get().register(this);
        mAdapter = new ShareOrderAdapter(getContext());
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(mAdapter);
        refreshLayout.setRefreshing(true);
        mPresenter.refresh();
    }

    @Override
    protected void initListener() {
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.refresh();
            }
        });
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                bundle.putInt(Constant.ORDER_ID, mAdapter.getData().get(position).getOrderId());
                bundle.putInt(Constant.INDEX, position);
                startActivity(TakeOrderDetailsActivity.class, bundle);
            }
        });
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.tv_take:
                        mPresenter.take(mAdapter.getData().get(position).getOrderId(), position);
                        break;
                }
            }
        });
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.loadMore();
            }
        }, recyclerView);
    }

    @OnClick(R.id.iv_headline_back)
    void onBack(){
        finish();
    }

    @Override
    public ShareOrderAdapter getAdapter() {
        return mAdapter;
    }

    @Override
    public RefreshLayout getRefreshLayout() {
        return refreshLayout;
    }
}
